﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Common.Utils;
using Telerik.WebControls;

public partial class sysadmin_DL_Project_AddClassTask : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    VOTOClassTasks voClassTask = new VOTOClassTasks();
    TOClassTasks objClassTask = new TOClassTasks();
    Course opCourse = new Course();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //{
        //    Script.RedirectURL(GetRefUrl());
        //}
        if (!IsPostBack)
        {
            InitPage();

            BinderAccessory();
            int CID = 0;
            if (Request.QueryString["CID"] != null)
            {
                CID = int.Parse(Request.QueryString["CID"].ToString());
                ViewState["CID"] = CID.ToString();
                voClass = objClass.Load(CID);
                ClassName = voClass.Name;
                BinderCourse(voClass);
            }
            int CourseID = 0;
            if (Request.QueryString["CourseID"] != null)
            {
                CourseID = int.Parse(Request.QueryString["CourseID"].ToString());
                ViewState["CourseID"] = CourseID.ToString();
                ddl_CourseID.SelectedValue = CourseID.ToString();

            }

            int CTID = 0;
            if (Request.QueryString["CTID"] != null)
            {
                CTID = int.Parse(Request.QueryString["CTID"].ToString());
                ViewState["CTID"] = CTID.ToString();
                BinderForm(CTID);
            }


        }
    }
    protected override void InitPage()
    {
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }


    //绑定课程
    private void BinderCourse(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        voCourselist = objCourselist.Load(vo.ClassCourseListID);
        string CourseIDsList = voCourselist.CourseID;
        string[] courseid = CourseIDsList.Split(',');
        string courseidlist = string.Empty;
        for (int i = 0; i < courseid.Length; i++)
        {
            string[] par = courseid[i].Split('#');
            if (par.Length == 2)
            {
                if (courseidlist.Length > 0)
                    courseidlist += ",";
                courseidlist += par[0];
            }
        }

        if (courseidlist.Length == 0)
            courseidlist = "0";

        string search = " Course_Id in (" + courseidlist + ")";
        DataTable dt = new DataTable();
        dt = opCourse.Query(search);

        ddl_CourseID.DataSource = dt;
        ddl_CourseID.DataTextField = "CourseName";
        ddl_CourseID.DataValueField = "Course_Id";
        ddl_CourseID.DataBind();

        ListItem li = new ListItem();
        li.Text = "综合";
        li.Value = "0";
        ddl_CourseID.Items.Insert(0, li);

    }
    //绑定附件
    private void BinderAccessory()
    {
        TOAccessory obja = new TOAccessory();
        DataTable dt = new DataTable();

        dt = obja.Query("");
        ddl_Accessory.DataSource = dt;
        ddl_Accessory.DataTextField = "Title";
        ddl_Accessory.DataValueField = "Accessory_ID";
        ddl_Accessory.DataBind();
    }

    private void BinderForm(int id)
    {
        voClassTask = objClassTask.Load(id);

        txt_Name.Text = voClassTask.TasksTitle;
        txt_Introduce.Text = voClassTask.TasksContent;
        ddl_CourseID.SelectedValue = voClassTask.Course_ID.ToString();

        ddl_Accessory.SelectedValue = voClassTask.Accessory.ToString();
        txt_EndData.Text = voClassTask.EndDate.ToString();


        if (voClassTask.Broswer)
        {
            radbtn_Browse_Yes.Checked = true;
            radbtn_Browse_No.Checked = false;
        }
        else
        {
            radbtn_Browse_Yes.Checked = false;
            radbtn_Browse_No.Checked = true;
        }
        if (voClassTask.SetTop)
        {
            radbtn_Top_Yes.Checked = true;
            radbtn_Top_No.Checked = false;
        }
        else
        {
            radbtn_Top_Yes.Checked = false;
            radbtn_Top_No.Checked = true;
        }
        if (voClassTask.SetHighLight)
        {
            radbtn_HighLight_Yes.Checked = true;
            radbtn_HighLight_No.Checked = false;
        }
        else
        {
            radbtn_HighLight_Yes.Checked = false;
            radbtn_HighLight_No.Checked = true;
        }
    }

    private bool CheckPageData()
    {
        string search = string.Format("TasksTitle = '{0}'", txt_Name.Text.Trim());
        if (ViewState["CTID"] != null)
        {
            search += "and ClassTasks_Id<>" + ViewState["CTID"].ToString();
        }
        if (objClassTask.Count(search) != 0)
        {
            lbl_msg_Error.Text = "<br />" + GetLanageValueByKey("msg_AddNewType_checkMsg", PowerCreator.LMS.Language.LanagetItemType.Page);
            return false;
        }
        if (string.IsNullOrEmpty(txt_EndData.Text))
        {
            lbl_msg_Error.Text = "请选择结束时间！";
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        
        if (ViewState["CTID"] == null)
        {
            voClassTask.TasksTitle = txt_Name.Text.Trim();
            voClassTask.TasksContent = txt_Introduce.Text.Trim();
            voClassTask.Course_ID = int.Parse(ddl_CourseID.SelectedValue);
            voClassTask.Accessory = int.Parse(ddl_Accessory.SelectedValue);
            voClassTask.ClassID = int.Parse(ViewState["CID"].ToString());
            voClassTask.EndDate = DateTime.Parse(txt_EndData.Text.Trim());
            if (radbtn_Browse_Yes.Checked == true)
                voClassTask.Broswer=true;
            if (radbtn_Browse_No.Checked == true)
                voClassTask.Broswer = false;
            if (radbtn_Top_Yes.Checked == true)
               voClassTask.SetTop = true;
            if (radbtn_Top_No.Checked == true)
                voClassTask.SetTop = false;
            if (radbtn_HighLight_Yes.Checked == true)
                voClassTask.SetHighLight = true;
            if (radbtn_HighLight_No.Checked == true)
                voClassTask.SetHighLight = false;

             voClassTask.CreatDate = DateTime.Now;
             PowerCreator.LMS.Web.Base.BasePage bpage=new PowerCreator.LMS.Web.Base.BasePage();
             voClassTask.Creator = bpage.GetCreatedByInfo().UserID;
            if (objClassTask.Add(voClassTask))
            {
                string url = "ManageClassTask.aspx?CID="  +ViewState["CID"].ToString();
                Script.RedirectURL(url);
            }
        }
        else
        {
            voClassTask=objClassTask.Load(int.Parse(ViewState["CTID"].ToString()));
            voClassTask.TasksTitle = txt_Name.Text.Trim();
            voClassTask.TasksContent = txt_Introduce.Text.Trim();
            voClassTask.Course_ID = int.Parse(ddl_CourseID.SelectedValue);
            voClassTask.Accessory = int.Parse(ddl_Accessory.SelectedValue);
            voClassTask.ClassID = int.Parse(ViewState["CID"].ToString());
            voClassTask.EndDate = DateTime.Parse(txt_EndData.Text.Trim());
            if (radbtn_Browse_Yes.Checked == true)
                voClassTask.Broswer = true;
            if (radbtn_Browse_No.Checked == true)
                voClassTask.Broswer = false;
            if (radbtn_Top_Yes.Checked == true)
                voClassTask.SetTop = true;
            if (radbtn_Top_No.Checked == true)
                voClassTask.SetTop = false;
            if (radbtn_HighLight_Yes.Checked == true)
                voClassTask.SetHighLight = true;
            if (radbtn_HighLight_No.Checked == true)
                voClassTask.SetHighLight = false;

            voClassTask.CreatDate = DateTime.Now;
            PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
            voClassTask.Creator = bpage.GetCreatedByInfo().UserID;
            if (objClassTask.Update(voClassTask))
            {
                string url = "ManageClassTask.aspx?CID="  +ViewState["CID"].ToString();
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageClassTask.aspx?CID="  +ViewState["CID"].ToString();
        Script.RedirectURL(url);
    }





    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //Session["pwd"] = txt_UserPwd.Text;
        foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
        {
            string Url = "";
            Url = WebUtils.WebRoot + "/fileManager/UploadFile/Accessory/";
            string Path = Server.MapPath(Url);

            //如果路径不存在，则创建
            if (System.IO.Directory.Exists(Path) == false)
            {
                System.IO.Directory.CreateDirectory(Path);
            }

            string filetype = file.GetExtension();
            if (!IsUploadFileType(filetype)) return;

            //组合路径，file.GetName()取得文件名
            string fileName = opCourse.GetUploadResourceFileName(file.GetExtension());

            Path = Path + fileName;
            Url = Url + fileName;
            //保存
            file.SaveAs(Path, true);

            VOTOAccessory vo = new VOTOAccessory();
            TOAccessory obj = new TOAccessory();
            vo.Url = Url;
            if (obj.Count(" Title='" + txt_Name.Text.Trim() + "' and Url='" + Url + "' ") == 0)
            {
                int num = obj.Count(" Title like '" + txt_Name.Text.Trim() + "%' ");
                if (num > 0)
                {
                    vo.Title = txt_Name.Text.Trim() + num.ToString();
                }
                else
                {
                    vo.Title = txt_Name.Text.Trim();
                }
                vo.Createtime = DateTime.Now;
                PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
                vo.Creator = bpage.GetCreatedByInfo().UserID;
                int Accessory_ID = System.Int32.MinValue;
                if (obj.Add(vo,ref Accessory_ID))
                {
                    ListItem li = new ListItem();
                    li.Text = vo.Title;
                    li.Value = Accessory_ID.ToString();
                    ddl_Accessory.Items.Insert(0, li);
                    ddl_Accessory.ClearSelection();
                    ddl_Accessory.SelectedValue = Accessory_ID.ToString();
                    //BinderAccessory();
                    //ddl_Accessory.SelectedValue = vo.Accessory_ID.ToString();
                }


            }
            //img_UserPic.ImageUrl = Url;
            //txt_UserPwd.Text = Session["pwd"].ToString();
            btn_Back.Enabled = true;
        }
    }

    //检查上传文件类型
    public bool IsUploadFileType(string extension)
    {
        string allowExtensions = "rar|zip|doc|xls|ppt";    //允许上传的类型
        if (!opCourse.IsAllowedExtension(extension, allowExtensions))
        {
            lbl_FileUpload_msg_FileType.Text = GetLanageValueByKey("AddCourseInfo_msg_m2", PowerCreator.LMS.Language.LanagetItemType.Page) + allowExtensions;
            return false;
        }
        return true;
    }


}